loadCartList();

async function loadCartList() {
  //购物车列表
  let uid = localStorage.getItem("uid");
  let {
    data
  } = await axios.get(CartListAPI, {
    params: {
      id: uid,
    },
  });
  console.log(data);
  let resArr = data.data.map(
    (v) =>
    `
   <tr class="itemlist2">
         <td valign="top">
           <input  type="checkbox" class="chkbox singleChange" onchange="singleChange()" >
           <a href="./details.html?id=${v.pid}" target="_blank" class="gimgCon"><img src="${v.pimg}"></a>
         </td>
         <td class="txtl">
           <p class="t"><a href="">${v.pname}</a></p>
           <p class="b">${v.pdesc}</p>
           <p class="b" style="color:#d20019;"></p>
         </td>
         <td>
           <span class="opt" onclick=subFn(${v.pid},this)><img src="img/minus.png"></span>
           <input type="text" value="${v.pnum}" class="num2" onchange="PnumChange(${v.pid},this)">
           <span class="opt opo" data-pid="${v.pid}"><img src="img/add.png"></span>
         </td>
         <td class="pprice-p">${v.pprice}</td>
         <td class="zongjia">${v.pprice*v.pnum}</td>
         <td><a href="" class=" blue" onclick="delFn(${v.pid})">删除</a></td>
       </tr>
   `
  );

  let listUl = document.querySelector(".biaodan");
  listUl.innerHTML = resArr.join("");
  let cartAddBtns = document.querySelectorAll(".opo");
  for (let i = 0; i < cartAddBtns.length; i++) {
    cartAddBtns[i].onclick = async function () {
      let ipt = this.previousElementSibling;
      let pnum = parseInt(ipt.value) + 1;
      let uid = localStorage.getItem("uid");
      let pid = this.getAttribute("data-pid");
      let params = {
        uid,
        pid,
        pnum
      };
      try {
        let {
          data
        } = await axios.get(CartUpdateAPI, {
          params
        });
        ipt.value = pnum;
        countFn();
        loadCartList()
      } catch (error) {
        alert("请检查网络");
      }
    };
  }
}

async function PnumChange(pid, txt) {
  if (txt.value < 1) {
    txt.value = 1
  }
  let uid = localStorage.getItem('uid');
  let pnum = txt.value;
  let data = await $.get(CartUpdateAPI, {
    uid: uid,
    pid: pid,
    pnum: pnum
  })
  countFn();
  loadCartList();
}
async function subFn(pid, btn) {
  let ipt = btn.nextElementSibling;
  if (parseInt(ipt.value) == 1) {
    alert("点不动了，别点了");
  } else {
    ipt.value = parseInt(ipt.value) - 1;
    let uid = localStorage.getItem("uid");
    let pnum = ipt.value;
    let params = {
      uid,
      pid,
      pnum
    };
    let {
      data
    } = await axios.get(CartUpdateAPI, {
      params
    });
    countFn();
    loadCartList()
  }
}

async function delFn(pid) {
  let uid = localStorage.getItem("uid");
  let {
    data
  } = await axios.get(CartDeleteAPI, {
    params: {
      uid,
      pid
    },
  });
  // loadCartList();
  countFn();
  btn.parentNode.remove();
}

function singleChange() {
  countFn();
  let singleIpts = document.querySelectorAll(".singleChange");
  let arr = [...singleIpts];
  let flag = arr.every((v) => v.checked == true);
  let allIpt = document.querySelector(".quanxuan");
  allIpt.checked = flag;
}

function countFn() {
  let singleIpts = document.querySelectorAll(".singleChange");
  // console.log(singleIpts);
  let arr = [...singleIpts];
  // console.log(arr);
  let resArr = arr.filter((v) => v.checked == true);
  // let quanxuan = document.querySelector(".chkbox");
  // quanxuan.checked = resArr;
  // console.log(resArr);
  let num = 0;
  resArr.forEach((singleIpt) => {
    let tr = singleIpt.parentNode.parentNode;
    let p = tr.querySelector(".pprice-p");
    let pnumIpt = tr.querySelector(".num2");
    console.log(pnumIpt);
    let count = p.innerHTML * pnumIpt.value;
    num = num + count;
    console.log(num);
  });
  let countSpan = document.querySelector(".count-span");
  countSpan.innerHTML = num;
  console.log(num);
}

function allChange() {
  let singleIpts = document.querySelectorAll(".singleChange");
  let allIpt = document.querySelector(".quanxuan");
  singleIpts.forEach((v) => (v.checked = allIpt.checked));
  countFn();
}